package com.zq.learing.sort;

import java.util.Comparator;

/**
 * @Author: zhaoqi
 * @Date: 2020/4/23 21:14
 * @Version 1.0
 */

//  冒泡排序

public class BubbleSort  implements Sorter {
    @Override
    public <T extends Comparable<T>> void Sort(T[] list) {
        boolean swapped =true;
        for (int i = 0 ;i< list.length && swapped; i++){
            swapped = true;
            for (int j = 0 ; j<list.length-i ;j++){
                if (list[j].compareTo(list[j+1]) > 0){
                    T temp = list[j];
                    list[j] = list[j+1];
                    list[j+1] =temp;
                    swapped =true;
                }
            }

        }
    }

    @Override
    public <T> void sort(T[] list, Comparator<T> comp) {
        boolean swapped =true;
        for (int i = 0 ;i< list.length && swapped; i++){
            swapped = true;
            for (int j = 0 ; j<list.length-i ;j++){
                if (comp.compare(list[j], list[j+1])> 0){
                    T temp = list[j];
                    list[j] = list[j+1];
                    list[j+1] =temp;
                    swapped =true;
                }
            }
        }
    }
}
